package cn.com.lms.business.attendance.web;

import cn.com.lms.business.attendance.persistence.entity.Clockin;
import cn.com.lms.business.attendance.service.ClockinService;
import cn.com.lms.business.core.util.SystemUserUtil;
import cn.com.lms.business.examine.persistence.entity.FbExamineInfo;
import cn.com.lms.business.examine.service.FbExamineInfoService;
import cn.com.lms.business.user.persistence.entity.SysUserVo;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "打卡", description = "打卡API")
@RestController
@RequestMapping("/clockin")
public class ClockinController {

    @Autowired
    private ClockinService clockinService;

    @Operation(summary = "打卡", responses = {
            @ApiResponse(responseCode = "1", description = "打卡成功"),
            @ApiResponse(responseCode = "0", description = "打卡失败")})
    @PostMapping(value = "/add")
    public ResponseResult<Clockin> addClockin(@RequestParam(value="sign", required=true) String sign) {
        SysUserVo currentUser = SystemUserUtil.getCurrentUser();
        Clockin clockin = new Clockin();
        clockin.setUserId(currentUser.getId());
        clockin.setUserAccount(currentUser.getAccount());
        clockin.setUserName(currentUser.getCname());
        clockin.setClockinTime(new Date());
        clockin.setSign(sign);
        clockinService.save(clockin);
        return ResponseResult.success();
    }

    @Operation(summary = "获取打卡时间", description = "获取打卡时间")
    @GetMapping(value = "/getClockinTime")
    public ResponseResult<Map<String, String>> getClockinTime(@RequestParam(value="date", required=true) String date) {
        Map<String, String> res = new HashMap<String, String>();
        List<Map<String, Object>> list1 = clockinService.getShangbanClockinTime(date);
        if(list1 != null && list1.size() > 0) {
            Map<String, Object> map = list1.get(0);
            if(map == null || map.get("clockinTime") == null || map.get("clockinTime").toString().trim().length() == 0) {
                res.put("shangbanTime", "");
            } else {
                res.put("shangbanTime", map.get("clockinTime").toString().substring(11,19));
            }
        } else {
            res.put("shangbanTime", "");
        }
        List<Map<String, Object>> list2 = clockinService.getXiabanClockinTime(date);
        if(list2 != null && list2.size() > 0) {
            Map<String, Object> map = list2.get(0);
            if(map == null || map.get("clockinTime") == null || map.get("clockinTime").toString().trim().length() == 0) {
                res.put("xiabanTime", "");
            } else {
                res.put("xiabanTime", map.get("clockinTime").toString().substring(11,19));
            }
        } else {
            res.put("xiabanTime", "");
        }
        return ResponseResult.success(res);
    }

}
